package com.mengye.control.userInfo;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mengye.bean.CourseInfoBean;
import com.mengye.bean.TeacherInfoBean;
import com.mengye.control.LoginControl;
import com.mengye.model.CourseInfoModel;
import com.mengye.model.UserInfoModel;
import com.mengye.test.CourseInfoService;
import com.mengye.utils.EasyFactory;
import com.mengye.utils.MyUtils;

@WebServlet("/UserInfoAjaxSavingControl")
@MultipartConfig(maxFileSize=10485760) // 限制上传文件大小，以bit为单位
public class UserInfoAjaxSavingControl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 将request中的form表单中的数据转换到实体类中
		TeacherInfoBean teacherInfoB = MyUtils.convertRequest2BeanWithFileUpload(request, TeacherInfoBean.class);
		teacherInfoB.setT_id(LoginControl.user_name);
		// 调用Model层进行用户信息保存的业务逻辑处理
		int i = EasyFactory.getNewInstance(UserInfoModel.class).updateInfo(teacherInfoB);
		CourseInfoService c = new CourseInfoService();
		List<TeacherInfoBean> lst = MyUtils.convertRequest2BeanWithFileUpload(request, CourseInfoModel.class).quetyPath(teacherInfoB);
		String lstPath = null;
		for (TeacherInfoBean teacherInfoBean : lst) {
			lstPath = "D:/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/Message/"+teacherInfoB.getT_excel();
		}
		
		List<CourseInfoBean> courseInfoLst= c.readExcelData(lstPath);
		int j = MyUtils.convertRequest2BeanWithFileUpload(request, CourseInfoModel.class).saveCourseInfo1(courseInfoLst);
		// 根据业务逻辑处理结果，进行结果页面的转发处理
		if(i >0) {
			// 新增成功
			// 返回响应内容
			response.getWriter().write("ok");
		}else {
			// 新增失败
			response.getWriter().write("ng");
		}
		
	}
}
